<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showCateDialog">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 树形表格 -->
      <vue-table class="treetable" :data='cateList' :columns='columns' :show-row-hover='false' show-index index-text='#' :selection-type='false' :expand-type='false' border>
        <template slot="likes" slot-scope="scope">
          <i class="el-icon-circle-check" v-if="scope.row.cat_deleted === false" style="color:lightgreen">
          </i>
          <i class="el-icon-circle-close" v-else style="color:lightgreen"></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>

        </template>
        <template slot="operation">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="warning" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </vue-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="querInfo.pagenum" :page-size="querInfo.pagesize" :page-sizes="[5, 8, 15, 20]" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
      <!-- 添加分类对话框 -->
      <el-dialog title="提示" :visible.sync="setShowCateDialog" width="50%" @close='addCateDialogColse'>
        <el-form ref="cateFormRefs" :model="addCateForm" :rules="cateFormRules" label-width="80px" >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级名称">
            <el-cascader
              v-model="selectedKeys"
              :options="parentCateList"
              :props="cascaderProps"
              clearable
              @change="cascaderChange">
            </el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="setShowCateDialog = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 请求参数
      querInfo: {
        type: [3],
        pagenum: 1,
        pagesize: 5
      },
      // 总数量
      total: 0,
      // 每页展示的数据
      cateList: [],
      // 为table指定列的定义
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'likes'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '排序',
          type: 'template',
          template: 'operation'
        }
      ],
      // 展示或隐藏添加分类对话框
      setShowCateDialog: false,
      // 添加分类的表单数据对象
      addCateForm: {
        cat_name: '',
        // 父级分类的id
        cat_pid: 0,
        // 分类的等级
        cat_level: 0
      },
      // 添加分类表单验证规则
      cateFormRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      // 父级分类的列表
      parentCateList: [],
      // 选中的父级分类id
      selectedKeys: [],
      // props配置对象
      cascaderProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取分类列表
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.querInfo })
      // 把数据总数赋值给 total
      this.total = res.data.total
      // 把数据列表赋值给 cateList
      this.cateList = res.data.result
    },
    // 监听当前页数据数量变化
    handleSizeChange (newSize) {
      this.querInfo.pagesize = newSize
      this.getCateList()
    },
    // 监听 pagenum 当前页码变化
    handleCurrentChange (newPage) {
      this.querInfo.pagenum = newPage
      this.getCateList()
    },
    // 点击按钮显示添加按钮对话框
    showCateDialog () {
      // 先获取父级分类列表
      this.getParentCateList()
      this.setShowCateDialog = true
    },
    // 获取父级分类列表
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      console.log(res)

      if (res.meta.status !== 200) return console.log('获取父级列表失败')
      this.parentCateList = res.data
    },
    // 监听级联选择器选项发生变化
    cascaderChange () {
      if (this.selectedKeys.length !== 0) {
        // 为父级id赋值
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        // 为当前分类的等级赋值
        this.addCateForm.cat_level = this.selectedKeys.length
        // 否则重置
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    // 点击添加确定按钮
    async addCate () {
      const { data: res } = await this.$http.post('categories', this.addCateForm)
      console.log(res)
      if (res.meta.status !== 201) return this.$Message.error('添加分类失败')
      this.$Message.success('添加分类成功')
      this.getCateList()
      this.setShowCateDialog = false
    },
    // 监听关闭添加分类对话框关闭事件
    addCateDialogColse () {
      this.$refs.cateFormRefs.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    }
  }
}
</script>

<style lang="less" scoped>
.treetable {
  margin-top: 15px;
}
.el-cascader{
  width: 100%;
}

</style>
